var globalSelectedRodada = null;

function Rodada(id){
    this.id = id;
    this.partidas = new Array();
}

Rodada.prototype.getKey = function(){
    return "rodada_"+this.id;
}

Rodada.prototype.addPartida = function(partida){
    this.partidas[partida.getKey()] = partida;
}

Rodada.prototype.createItemList = function(){
    var item = document.createElement('DIV');
    item.className = 'item button';
    
    item.appendChild(document.createTextNode(this.id+'º rodada'));
    item.setAttribute('DATA-KEY', this.getKey());
    
    item.addEventListener('click', requestRodadaData, true);
    
    return item;
}

function requestRodadaData(event){
    var key = event.target.getAttribute('DATA-KEY');
    globalSelectedRodada = globalRodadas[key];
    preloaderAnimation();
    
    var parameters = new Array();
    parameters['request'] = 'gols';
    parameters['rodada'] = globalSelectedRodada.id;
    
    var communicator = new Communicator();
    communicator.addParametersList(parameters);
    communicator.sendRequest(receiveRequestRodadaDataHandler);
    document.getElementById('tabela').className = 'invisible';
}

function receiveRequestRodadaDataHandler(response){
    var responseObj = eval(response);
    
    //Gols
    receiveGolsHandler(responseObj);
    
    globalSelectedRodada.drawRodadaInfo();
    preloaderAnimation();
}


Rodada.prototype.drawRodadaInfo = function(){
    var fragment = document.createDocumentFragment();
    
    var rodadaTitle = document.createElement('H3');
    rodadaTitle.className = 'title';
    rodadaTitle.appendChild(document.createTextNode(this.id+"º Rodada"));
    
    fragment.appendChild(rodadaTitle);
    
    for(var key in this.partidas){
        var partida = this.partidas[key];
        fragment.appendChild(partida.createItem());
    }
    
    var partidasInfo = document.getElementById('partidasInfo');
    
    partidasInfo.className = '';
    clearElement(partidasInfo);
    partidasInfo.appendChild(fragment);
}